home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / TEXAS XFCNs ƒ / catFiles.z1.c next >
Encoding:
C/C++ Source or Header  |  1987-12-29  |  4.5 KB  |  224 lines  |  [TEXT/KAHL]

  1. /* a HyperCard XFCN that joins together (catenates, concatenates)
  2.  * a chosen set of text files, by giving the user std files dialog
  3.  * repeatedly -- first to pick the 'base' file, then to pick those
  4.  * other files to append to the end of the 'base' file....
  5.  *
  6.  * call it as HyperTalk function:     catFiles (x0, y0)
  7.  *
  8.  * where x0, y0 are the screen coordinates to base the dialog boxes
  9.  * on ... 0,0 works for a Mac Plus....
  10.  *
  11.  * make it XFCN number 2222 and name it "catFiles"
  12.  *
  13.  * 871222 ^z
  14.  */
  15.  
  16.  
  17. #include <MacTypes.h>
  18. #include <OSUtil.h>
  19. #include <FileMgr.h>
  20. #include <StdFilePkg.h>
  21. #include <WindowMgr.h>
  22. #include <HyperXCmd.h>
  23.  
  24. pascal void main (XCmdBlockPtr paramPtr);
  25. int GetFileZ (Str255 *fn, int *vRef, int x0, int y0);
  26. void pStrCopy (char *p1, char *p2);
  27. void appendFile (int refNum0, Str255 *fnX, int vRefX);
  28. long atol (char *s);
  29.  
  30. /* set up buffer of size 25,600 bytes (= 512 * 50) for copying through;
  31.  * ZBUFSIZ must be an integer, < 32768
  32.  */
  33. #define ZBUFSIZ  25600
  34.  
  35.  
  36. pascal void main (paramPtr)
  37.   XCmdBlockPtr paramPtr;
  38.   {
  39.     WindowRecord w_record;
  40.     WindowPtr info_window;
  41.     Rect b_rect;
  42.     Str255 fn0, fnX;
  43.     int vRef0, vRefX, refNum0, x0, y0;
  44.  
  45.     if (paramPtr->paramCount != 2)
  46.       {
  47.           SysBeep (10);
  48.           return;
  49.       }
  50.     x0 = atol (*(paramPtr->params[0]));
  51.     y0 = atol (*(paramPtr->params[1]));
  52.  
  53.     b_rect.top = 30 + y0;
  54.     b_rect.left = 12 + x0;
  55.     b_rect.bottom = 52 + y0;
  56.     b_rect.right = 500 + x0;
  57.     info_window = NewWindow (&w_record, &b_rect, "\p", (Boolean)1, dBoxProc,
  58.         (WindowPtr)-1, (Boolean)0, (long)0);
  59.     ShowWindow (info_window);
  60.     SetPort (info_window);
  61.     TextFont (0);
  62.     b_rect.top = 0;
  63.     b_rect.left = 0;
  64.     b_rect.bottom = 22;
  65.     b_rect.right = 488;
  66.  
  67.     EraseRect (&b_rect);
  68.     MoveTo ( 4,15);
  69.     DrawString ("\pChoose a base file -- other files will be appended to it.");
  70.  
  71.     if (GetFileZ (&fn0, &vRef0, x0, y0))
  72.       {
  73.         if (FSOpen (fn0, vRef0, &refNum0) != noErr)
  74.           {
  75.               SysBeep (10);
  76.               CloseWindow (info_window);
  77.             return;
  78.           }
  79.         EraseRect (&b_rect);
  80.         MoveTo (4, 15);
  81.         DrawString ("\pChoose files to append to the base -- 'Cancel' when done.");
  82.         while (GetFileZ (&fnX, &vRefX, x0, y0))
  83.             appendFile (refNum0, &fnX, vRefX);
  84.         FSClose (refNum0);
  85.       }
  86.  
  87.     CloseWindow (info_window);
  88.     return;
  89.   }
  90.  
  91.  
  92. /* following variables and routine do the standard files dialog
  93.  * to get the name of the file to use ... cribbed from the MiniEdit
  94.  * example that comes with LSC....  put the dialog box someplace
  95.  * reasonable, nothing complex ....
  96.  */
  97.  
  98. int GetFileZ (fnp, vRefp, x0, y0)
  99.   Str255 *fnp;
  100.   int *vRefp, x0, y0;
  101.   {
  102.     SFTypeList myTypes;
  103.     Point SFGwhere;
  104.     SFReply reply;
  105.  
  106.     SFGwhere.v = 90 + y0;
  107.     SFGwhere.h = 82 + x0;
  108.     myTypes[0] = 'TEXT';
  109.     
  110.     SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply);
  111.     
  112.     if (reply.good)
  113.       {
  114.         pStrCopy( (char *)reply.fName, (char *)fnp);
  115.         *vRefp = reply.vRefNum;
  116.         return (1);
  117.       }
  118.     else return (0);
  119.   }
  120.  
  121.  
  122. /* routine to copy a pascal string from one place to another.... used in
  123.  * above Standard Files routine.... from LSC library....
  124.  */
  125.  
  126. void pStrCopy (p1, p2 )
  127.   register char *p1, *p2;
  128.   {
  129.     register int len;
  130.     
  131.     len = *p2++ = *p1++;
  132.     while (--len >= 0)
  133.         *p2++ = *p1++;
  134.     return;
  135.   }
  136.  
  137.  
  138. /* routine to append the contents of file X to file 0....does some real
  139.  * simple error checking and (because of constraints while working within
  140.  * HyperCard, and my naivete) just beeps and closes the current file if
  141.  * an error is found....
  142.  */
  143.  
  144. void appendFile (refNum0, fnXp, vRefX)
  145.   int refNum0, vRefX;
  146.   Str255 *fnXp;
  147.   {
  148.     int refNumX, err;
  149.     long count;
  150.     char buf[ZBUFSIZ];
  151.     
  152.     if (FSOpen (fnXp, vRefX, &refNumX) != noErr)
  153.       {
  154.           SysBeep (10);
  155.           FSClose (refNumX);
  156.           return;
  157.       }
  158.     if (SetFPos (refNumX, fsFromStart, 0L) != noErr)
  159.       {
  160.           SysBeep (10);
  161.           FSClose (refNumX);
  162.           return;
  163.       }
  164.     if (SetFPos (refNum0, fsFromLEOF, 0L) != noErr)
  165.       {
  166.           SysBeep (10);
  167.           FSClose (refNumX);
  168.           return;
  169.       }
  170.     for (;;)
  171.       {
  172.         count = ZBUFSIZ;
  173.         err = FSRead (refNumX, &count, buf);
  174.         if (err != noErr && err != eofErr)
  175.           {
  176.               SysBeep (10);
  177.               FSClose (refNumX);
  178.               return;
  179.           }
  180.         if (count == 0)
  181.             break;
  182.         if (FSWrite (refNum0, &count, buf) != noErr)
  183.           {
  184.               SysBeep (10);
  185.               FSClose (refNumX);
  186.               return;
  187.           }
  188.       }
  189.     FSClose (refNumX);
  190.     return;
  191.   }
  192.   
  193.  
  194. /* function to convert alphabetic string to a long integer ... from LSC
  195.  * library.... simplified to avoid using isspace() & isdigit() ....
  196.  */
  197.  
  198. long atol (s)
  199.   register char *s;
  200.   {
  201.     register char signflag = 0;
  202.     register long r = 0;
  203.  
  204.     while ((*s == ' '))
  205.         s++;
  206.         
  207.     if (*s == '-')
  208.       {
  209.         signflag = 1;
  210.         s++;
  211.       }
  212.     else if (*s == '+')
  213.          s++;
  214.  
  215.     while (*s >= '0' && *s <= '9') 
  216.         r = r * 10 + (*s++ - '0');
  217.     
  218.     return (signflag ? -r : r);
  219. }
  220.  
  221.  
  222.  
  223.  
  224.